package com.bit.book.service;

import com.bit.book.dao.BookDao;
import com.bit.book.enmus.BookStaus;
import com.bit.book.map.BookInfoMapper;
import com.bit.book.map.UserinfoMapper;
import com.bit.book.model.BookInfo;
import com.bit.book.model.PageRequest;
import com.bit.book.model.PageResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BookService {
    @Autowired
    BookDao bookDao ;
    @Autowired
    BookInfoMapper bookInfoMapper;

    public List<BookInfo> getList(){

        List<BookInfo> bookInfos = bookDao.mockData();
        for (BookInfo bookInfo: bookInfos){
            if (bookInfo.getStatus()==2){
                bookInfo.setStatusCN("不可借阅");
            }else {
                bookInfo.setStatusCN("可借阅");
            }
        }
        return bookInfos;
    }


    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count=bookInfoMapper.count();
        List<BookInfo> books=bookInfoMapper.queryBookListByPage(pageRequest);
        for(BookInfo book:books){
           // log.info("status{}:",book.getStatus());
            book.setStatusCN(BookStaus.getNameByCode(book.getStatus()).getDesc());
            //log.info("statusCN:{}",book.getStatusCN());
        }
        return new PageResult<>(count,pageRequest,books);
    }

    public BookInfo queryBookById(Integer id) {
        return bookInfoMapper.queryBookListById(id);
    }

    public void updateBook(BookInfo bookInfo) {
         bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
